package com.tust.controller;

import com.tust.domain.Grade;
import com.tust.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class GradeController {
    private String userName;
    @Autowired
    private GradeService gradeService;

    @ResponseBody
    @RequestMapping("/selectGradeAll.do")
    public List<Grade> selectGradeAll(String userName) {
        List<Grade> grades = gradeService.selectGradeAll();
        this.userName = userName;
        return grades;
    }

    @ResponseBody
    @RequestMapping("/insertGrade.do")
    public String insertGrade(String studentNum, String courseNum,
                              String examTimes, String regularGrade,
                              String examGrade, String totalGrade) {
        String msg =null;
        Grade newGrade = new Grade(studentNum, courseNum, examTimes,
                regularGrade, examGrade, totalGrade);
        List<Grade> grade = gradeService.selectGrade(newGrade);
        if (grade.size() == 1) {//成绩存在
            msg = "gradeExist";
        } else {
            if (gradeService.insertGrade(newGrade) == 1) {//插入成功
                msg = "insertSuccess";
            } else {//插入失败
                msg = "insertFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/updateGrade.do")
    public String updateGrade(String oldStudentNum, String oldCourseNum,
                              String oldExamTimes, String oldRegularGrade,
                              String oldExamGrade, String oldTotalGrade,
                              String studentNum, String courseNum,
                              String examTimes, String regularGrade,
                              String examGrade, String totalGrade) {
        String msg = null;
        Grade newGrade = new Grade(studentNum, courseNum, examTimes,
                regularGrade, examGrade, totalGrade);
        List<Grade> grade = gradeService.selectGrade(newGrade);
        if (grade.size() == 1 && (!oldStudentNum.equals(studentNum)
                || !oldCourseNum.equals(courseNum))) {//新成绩存在
            msg = "newGradeExist";
        } else {
            Grade oldGrade = new Grade(oldStudentNum, oldCourseNum, oldExamTimes,
                    oldRegularGrade, oldExamGrade, oldTotalGrade);
            if (gradeService.updateGrade(oldGrade, newGrade) == 1) {//修改成功
                msg = "updateSuccess";
            } else {//修改失败
                msg = "updateFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/selectGrade.do")
    public List<Grade> selectGrade(String studentNum, String courseNum,
                                   String examTimes, String regularGrade,
                                   String examGrade, String totalGrade) {
        List<Grade> grade = null;
        if (!studentNum.equals("")) {//查询学号
            grade = gradeService.selectGradeByStudentNum(studentNum);
        } else if (!courseNum.equals("")) {//查询课程号
            grade = gradeService.selectGradeByCourseNum(courseNum);
        } else if (!examTimes.equals("")) {//查询考试次数
            grade = gradeService.selectGradeByExamTimes(examTimes);
        } else if (!regularGrade.equals("")) {//查询平时成绩
            grade = gradeService.selectGradeByRegularGrade(regularGrade);
        } else if (!examGrade.equals("")) {//查询考试成绩
            grade = gradeService.selectGradeByExamGrade(examGrade);
        } else if (!totalGrade.equals("")) {//查询总评成绩
            grade = gradeService.selectGradeByTotalGrade(totalGrade);
        }
        return grade;
    }

    @ResponseBody
    @RequestMapping("/deleteGrade.do")
    public String deleteGrade(String studentNum, String courseNum,
                              String examTimes, String regularGrade,
                              String examGrade, String totalGrade) {
        String msg = null;
        Grade grade = new Grade(studentNum, courseNum, examTimes,
                regularGrade, examGrade, totalGrade);
        if (gradeService.deleteGrade(grade) == 1) {//删除成功
            msg = "deleteSuccess";
        } else {//删除失败
            msg = "deleteFalse";
        }
        return msg;
    }

    @RequestMapping("/studentGradeRefresh.do")
    public ModelAndView studentGradeRefresh() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("studentGrade");
        mv.addObject("userName", userName);
        mv.addObject("modelName", "学生课程成绩");
        mv.addObject("animate","false");
        return mv;
    }
}
